<?php

class Field {

    private $ID;
    private $name;
    private $position;
    private $length;
    private $type;
    private $description;
    private $tableID;
    private $isPrimary;

    public function __construct() {
        $num = func_num_args();
        $args = func_get_args();
        switch ($num) {
            case 0:
                $this->__construct0();
                break;
            case 1:
                $this->__construct1($args[0]);
                break;
            default:
                throw new Exception('calling wrong constructor');
        }
    }

    private function __construct0() {
        NULL;
    }

    private function __construct1($fieldID) {

        $query = new Query();
        $query->select("*");
        $query->where("ID", $fieldID);
        $query->table("fields");
        $query->build('SELECT');
        $query->exec();
        $record = $query->fetchRow();

        $this->ID = $record['ID'];
        $this->name = $record['name'];
        $this->position = $record['position'];
        $this->length = $record['length'];
        $this->type = $record['type'];
        $this->description = $record['description'];
        $this->tableID = $record['tableID'];
        $this->isPrimary = $record['isPrimary'];
    }

    public function convertToArray() {
        return get_object_vars($this);
    }

    public function __toString() {
        echo '<pre>';
        print_r($this);
        echo '</pre>';
    }

    

    public function getID() {
        return $this->ID;
    }

    public function setID($ID) {
        $this->ID = $ID;
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getPosition() {
        return $this->position;
    }

    public function setPosition($position) {
        $this->position = $position;
    }

    public function getLength() {
        return $this->length;
    }

    public function setLength($length) {
        $this->length = $length;
    }

    public function getType() {
        return $this->type;
    }

    public function setType($type) {
        $this->type = $type;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setDescription($description) {
        $this->description = $description;
    }

    public function getTableID() {
        return $this->tableID;
    }

    public function setTableID($tableID) {
        $this->tableID = $tableID;
    }

    public function getIsPrimary() {
        return $this->isPrimary;
    }

    public function setIsPrimary($isPrimary) {
        $this->isPrimary = $isPrimary;
    }

    public function add() {
        $query = new Query();
        $query->val("name", $this->name);
        $query->val("position", $this->position);
        $query->val("length", $this->length);
        $query->val("type", $this->type);
        $query->val("description", $this->description);
        $query->val("tableID", $this->tableID);
        $query->val("isPrimary", $this->isPrimary);
        $query->table("fields");
        $query->build('INSERT');
        $query->exec();
        $this->ID = $query->getLastInsertID();
    }

    public function update() {

        $query = new Query();
        $query->val("name", $this->name);
        $query->val("position", $this->position);
        $query->val("length", $this->length);
        $query->val("type", $this->type);
        $query->val("description", $this->description);
        $query->val("tableID", $this->tableID);
        $query->val("isPrimary", $this->isPrimary);
        $query->where("ID", $this->ID);
        $query->table("fields");
        $query->build('UPDATE');
        $query->exec();
    }

    public function delete() {
        $query = new Query();
        $query->where('ID', $this->ID);
        $query->table("fields");
        $query->build('DELETE');
        $query->exec();
    }

}

?>
